package com.dave.admin.modular.boss.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dave.admin.common.exception.AssertionEx;
import com.dave.admin.common.result.CommonPage;
import com.dave.admin.common.result.CommonResult;
import com.dave.admin.component.log.SysLog;
import com.dave.admin.modular.boss.dto.UpdateAdminPasswordParam;
import com.dave.admin.modular.boss.dto.UserAdminInfoParam;
import com.dave.admin.modular.boss.dto.UserAdminVo;
import com.dave.admin.modular.boss.entity.UserAdmin;
import com.dave.admin.modular.boss.service.IUserAdminService;
import com.dave.admin.util.QueryWrapperUtil;
import com.dave.admin.util.SecurityUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 后台用户管理
 *
 * @author Dave
 */
@RestController
@AllArgsConstructor
@RequestMapping("/boss/admin")
public class UserAdminController extends BaseController {

    @Override
    public void setPermissionPrefix() {
        super.permissionPrefix = "BossAccount";
    }

    private final IUserAdminService iUserAdminService;

    /**
     * 获取当前登录用户信息数据
     */
    @GetMapping("/info")
    public CommonResult info() {
        UserAdminVo userAdminVo = new UserAdminVo();
        BeanUtils.copyProperties(SecurityUtil.getUserAdmin(), userAdminVo);
        return CommonResult.success(userAdminVo);
    }


    /**
     * 获取用户基本信息列表
     */
    @GetMapping("/page")
    public CommonResult page(Page page, UserAdmin userAdmin) {
        QueryWrapper queryWrapper = QueryWrapperUtil.getQueryWrapper(userAdmin);
        queryWrapper.select(
                UserAdmin.ID,
                UserAdmin.USERNAME,
                UserAdmin.NICK_NAME,
                UserAdmin.LOCATION,
                UserAdmin.STATUS,
                UserAdmin.AVATAR,
                UserAdmin.CREATE_TIME);

        IPage iPage = iUserAdminService.page(page, queryWrapper);
        return CommonResult.success(CommonPage.restPage(iPage));
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return Results 响应数据
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Detail')")
    public CommonResult getById(@PathVariable("id") String id) {
        return CommonResult.success(iUserAdminService.getById(id));
    }


    /**
     * 通过id删除
     *
     * @param id id
     * @return Results 响应数据
     */
    @SysLog("通过ID删除用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasPermission(#this.getThis(),'-Delete')")
    public CommonResult removeById(@PathVariable String id) {
        return CommonResult.success(iUserAdminService.removeById(id));
    }

    /**
     * 更新用户
     */
    @SysLog("通过ID更新用户")
    @PutMapping
    public CommonResult updateById(@RequestBody UserAdminInfoParam userAdminInfoParam) {
        UserAdmin userAdmin = new UserAdmin();
        BeanUtils.copyProperties(userAdminInfoParam, userAdmin);
        iUserAdminService.updateById(userAdmin);
        return CommonResult.success();
    }

    /**
     * 新增指定用户信息
     */
    @SysLog("添加用户")
    @PostMapping
    @PreAuthorize("hasPermission(#this.getThis(),'-Add')")
    public CommonResult save(@RequestBody UserAdminVo userAdminVo) {
        UserAdmin userAdmin = iUserAdminService.getOne(new QueryWrapper<UserAdmin>().eq(UserAdmin.USERNAME, userAdminVo.getUsername()));
        AssertionEx.isNull(userAdmin, "该登录名已使用");
        iUserAdminService.add(userAdminVo);
        return CommonResult.success();
    }

    /**
     * 修改密码
     */
    @SysLog("修改密码")
    @PostMapping(value = "/updatePassword")
    public CommonResult updatePassword(@RequestBody UpdateAdminPasswordParam updatePasswordParam) {
        iUserAdminService.updatePassword(updatePasswordParam);
        return CommonResult.success();
    }

    /**
     * 初始化密码
     */
    @SysLog("初始化密码")
    @PreAuthorize("hasPermission(#this.getThis(),'-InitPassword')")
    @PatchMapping("/initPassword/{id}")
    public CommonResult initPassword(@PathVariable String id) {
        iUserAdminService.initPassword(id);
        return CommonResult.success();
    }
}
